use([
    "public/javascripts/service/bill_detail_tradeService.js",
    "public/javascripts/widget/slider/slider.js",
    "public/javascripts/widget/prompt/prompt.js",
    "public/javascripts/widget/calander/calander.js",
    "public/javascripts/widget/select/select.js"
  ], function(bill_detail_tradeService, SliderComponent, PromptComponent, Calander, selectFactory) {
    var PageComponent = React.createClass({
        componentDidMount: function() {
            Calander.init({
                ele: ['#beginDate', '#endDate'],
                isClear: true,
            });
            $("#beginDate").change(function(e){
                self.refs.startEffectTime.setValue(e.target.value);
            });
            $("#endDate").change(function(e){
                self.refs.endEffectTime.setValue(e.target.value);
            });
            this.getSubAccount();
        },
        getInitialState: function() {
            return this.props.data ? this.props.data : {}
        },
        searchOrders: function(param) {
            if(!param) param = {};
            if(param.pageIndex) {
                this.extend(param, this.state.searchData);
            }

            if (this.state.userList && param.custUserId) {
                this.state.userList.forEach(function (item) {
                    if (item.nameCode == param.custUserId) {
                      param.custUserId = item.id;
                      return;
                    }
                });
            }
            if(param.custUserId){
              param.custUserId = param.custUserId == '全部'? '' : param.custUserId;
            }

            param.billNo = this.state.billNo;
            param.orderNo = param.orderNo ? param.orderNo.trim() : '';
            var _this = this;

            var config = {
              'urlParams': param,
              'seatParams': {
                'interType': _this.state.interType
              }
            }
            bill_detail_tradeService.searchOrders(config, function(res) {
                if(res.errorCode == 0){
                    _this.setState({
                        list: res.data,
                        total: res.pagination.totalResult
                    });
                }
            });
        },
        extend : function(dist, source){
            for(var i in source) if(source[i]) dist[i] = source[i];
        },
        getSubAccount: function() {
            var _this = this;
            bill_detail_tradeService.getSubAccount('', function(res) {
                if(!res.data) return;
                res.data.forEach(function(item){
                    item.nameCode = item.fullName + '(' + item.userName + ')';
                });
                _this.setState({ userList: res.data });
                selectFactory({
                    data: res,
                    id: "username",
                    offset: -300,
                    isSearch: true,
                    isSaveInputVal:true,
                    defaultText: '全部',
                    showTextField: 'nameCode',
                    closeLocalSearch:true,
                    pagination: true,
                    searchPlaceHoder: '请输入姓名或用户名',
                    attrTextModel:function(fullName){
                        _this.refs.custUserId.setValue(fullName);
                    },
                    onSearchValueChange: function(attachEvent, data, currentPage) {
                        var config = {
                            q: data || '',
                            pageIndex: currentPage || 1,
                            pageSize: 10
                        };
                      bill_detail_tradeService.getSubAccount(config, function (res) {
                          //if (!res.data.length) return;
                          res.data.forEach(function(item){
                              item.nameCode = item.fullName + '(' + item.userName + ')';
                          });
                          _this.setState({ userList: res.data });
                          attachEvent.setData(res);
                      });
                    }
                });
            });
        },
        submit : function(data) {
            this.refs.form.setFormPristine();
            if (data.orderNo) {
                searchData = { orderNo: data.orderNo };
            } else {
                searchData = data;
            }
            this.setState({ searchData: searchData, current: 1 });
            this.searchOrders(searchData);
        },
        resetForm : function() {
            this.refs.form.setFormPristine();
            this.refs.form.reset();
            this.setState({ searchData: '' });
            this.searchOrders();
        },
        onPageChange : function(pageIndex,pageSize){
            var param = {pageSize: pageSize, pageIndex:pageIndex};
            this.setState({ pageSize: pageSize, current: pageIndex });
            this.extend(param, this.state.searchData || {});
            this.searchOrders(param);
        },
        render:function() {
            var Browser = typeof window != 'undefined';
            var component = this;
            return ~init;
        }
    });
    if(typeof ReactDOM != "undefined") {
        ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
    }
    module.exports = PageComponent;
});
